<?php
class Monhoc_model extends CI_Model{
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
   private function is_exist($colum,$value) {
            $this->db->select('id');
            $this->db->from('gs7s_monhoc');
            $this->db->where($colum,$value);
            $query=$this->db->get();
            if ($query->num_rows()>0) return TRUE;
            return FALSE;
        }
        public function Create($name,$id_nganh){
            if ($this->is_exist('TenMonHoc',$name)===TRUE) return FALSE;
            $data=array(
                'TenMonHoc'=>$name,
                'NganhHoc_id'=>$id_nganh,
                'anhien'=>FALSE
            );
            $this->db->insert('gs7s_monhoc',$data);
            return TRUE;
        }
        public function edit($id,$name,$id_nganh,$status) {
            if ($this->is_exist('id', $id)===FALSE) return FALSE;
            $data=array(
                'TenMonHoc'=>$name,
                'NganhHoc_id'=>$id_nganh,
                'anhien'=>$status
            );
            $this->db->update('gs7s_monhoc', $data, array('id'=>$id));
            return TRUE;
        }
        public function edit_name($id,$name,$id_nganh) {
            if ($this->is_exist('id', $id)===FALSE) return FALSE;
            $data=array(
                'TenMonHoc'=>$name,
                'NganhHoc_id'=>$id_nganh
            );
            $this->db->update('gs7s_monhoc', $data, array('id'=>$id));
            return TRUE;
        }
        public function edit_status($id,$status) {
            if ($this->is_exist('id', $id)===FALSE) return FALSE;
            $data=array(
                'anhien'=>$status
            );
            $this->db->update('gs7s_monhoc', $data, array('id'=>$id));
            return TRUE;
        }
        public function load_id_nganh($id) {
            $this->db->select ('TenMonHoc,anhien');
            $this->db->from('gs7s_monhoc');
            $this->db->where ('NganhHoc_id',$id);
            $query=$this->db->get();
            return $query->result_array();
        }
        public function load_id($id='all') {
            $this->db->select ('gs7s_nganhhoc.TenNganhHoc,gs7s_monhoc.id,gs7s_monhoc.TenMonHoc,gs7s_monhoc.anhien');
            $this->db->from('gs7s_monhoc');
            if ($id!='all') $this->db->where ('gs7s_monhoc.id',$id);
            $this->db->join('gs7s_nganhhoc', 'gs7s_nganhhoc.id=gs7s_monhoc.NganhHoc_id');
            $this->db->order_by('gs7s_nganhhoc.TenNganhHoc ASC');
            $query=$this->db->get();
            return $query->result_array();
        }
        public function load_status($status) {
            $this->db->select ('gs7s_nganhhoc.TenNganhHoc,gs7s_monhoc.id,gs7s_monhoc.TenMonHoc,gs7s_monhoc.anhien');
            $this->db->from('gs7s_monhoc');
            $this->db->where ('gs7s_monhoc.anhien',$status);
            $this->db->join('gs7s_nganhhoc', 'gs7s_nganhhoc.id=gs7s_monhoc.NganhHoc_id');
            $this->db->order_by('gs7s_nganhhoc.TenNganhHoc ASC');
            $query=$this->db->get();         
            return $query->result_array();
        }
        public function delete($id) {
            return $this->db->delete('gs7s_monhoc', array('id'=>$id));
        }
}
